package com.example.springai.repository;


import com.example.springai.mapper.ChatHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 会话历史保存到数据库
 */
@Repository
public class InDatabaseChatHistoryRepository implements ChatHistoryRepository{

    @Autowired
    private ChatHistoryMapper chatHistoryMapper;


    @Override
    public void save(String type, String chatId) {
        if (!ifExists(type, chatId)){
            chatHistoryMapper.save(type, chatId);
        }
    }


    public boolean ifExists(String type, String chatId) {
        List<String> chatIds = getChatIds(type);
        return chatIds.contains(chatId);
    }

    @Override
    public void delete(String type, String chatId) {

    }





    @Override
    public List<String> getChatIds(String type) {
        List<String> chatIds = chatHistoryMapper.getChatIds(type);
        if(CollectionUtils.isEmpty(chatIds)){
            return List.of();
        }
        return chatIds;
    }
}
